<?php
include_once dirname(dirname(__FILE__)).'/__queue_global.php';
include_once(SERVER_ROOT_PATH.'/include/ml_function_lib.php');            //Ⱥ���ͻ����
class ml_cron_album_count 
{
    const QUEUE_NAME = ML_CRONPHP_ALBUM_COUNT;
    private $_oMeilaPic;
    private $_oRdsAlbumCount;
    private $_oRdsCount;
    private $_oMcWeiboShare;
    public function __construct(){
        $this->_oMeilaPic = new ml_model_admin_dbMeilaPic();
        $this->_oRdsAlbumCount = new ml_model_album_rdsCount();
        $this->_oRdsCount = new ml_model_rdsCount();
        $this->_oMcWeiboShare = new ml_model_album_mcWeiboShare();
        $this->main();
    }
    public function main(){
        $class_list = $this->_get_class_list();
        if(!$class_list)
            die("无class数据");
        foreach($class_list as $class){
            $rs = $this->_deal_album_data($class['class_id']);
            var_dump($rs);
        }
        exit("完成");
    }
    private function _deal_album_data($class_id){
        $album_list = ml_tool_getdata::lbJsonData('class_'.$class_id, 'albumlist');
        foreach($album_list['album_list'] as $album){
            $this->_deal_pic_data($album['album_id'],$class_id);
        }
        return true;
    }
    private function _deal_pic_data($album_id, $class_id){
        $pic_list = ml_tool_getdata::lbJsonData('albumpic_album'.$album_id, 'albumpic/class_'.$class_id);
        foreach($pic_list['piclist'] as $pic){
            $pic_ids[] = $pic['pic_id'];
            if((!empty($pic['point'])) && count($pic['point'])>0){
                foreach ($pic['point'] as $point){
                    $rids[] = $point['rid'];
                }
            }
        }
        $rs = $this->_oMeilaPic->getPicByPids($pic_ids);
        if(!$rs)
            return false;
        $pic_list = $this->_oMeilaPic->get_data();

        $like_cnt = 0;
        $share_cnt = 0;
        foreach($pic_list as $pic){
            $like = $pic['like_visitor'] + $pic['like_user'];
            $like_cnt += gb_function_lib::getVitrulCnt($pic['pic_id'], (int)$like);
            $share_cnt += (int)$this->_oMcWeiboShare->getShare($class_id.'-'.$album_id.'-'.$pic['pic_id']);
        }
        if(isset($rids)){
            $goods_like_cnt = $this->_get_like_cnt_by_rids($rids);
            $like_cnt += $goods_like_cnt;
        }
        $rs = $this->_oRdsAlbumCount->setAlbumWeiboShareCnt($album_id, $share_cnt);
        $rs = $this->_oRdsAlbumCount->setAlbumLikeCnt($album_id, $like_cnt);
        return true;
    }
    
    private function _get_like_cnt_by_rids($rids){
        $rs = $this->_oRdsCount->getAttitudeCount($rids);
        if(!$rs)
            return false;
        $goods_list = $this->_oRdsCount->get_data();
        $like_cnt = 0;
        foreach ($goods_list as $goods){
            $like_cnt += (int)$goods;
        }
        return $like_cnt;
    }
    public function _get_class_list(){
        $class_list = ml_tool_getdata::lbJsonData('class');
        return $class_list['list'];
    }
}

ml_run_queue_check(ml_cron_album_count::QUEUE_NAME);
$obj = new ml_cron_album_count();
